//
//  HZGoodsDetailHeadModel.m
//  app
//
//  Created by 刘华舟 on 15/5/26.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsDetailHeadModel.h"
#import "NSDictionary+SafetyGet.h"
#import "UILabel+Conveniently.h"

@implementation HZGoodsDetailHeadModel

+ (instancetype)modelWithDictionary:(NSDictionary *)dict{
    return [[HZGoodsDetailHeadModel alloc] initModelWithDictionary:dict];
}

- (instancetype)initModelWithDictionary:(NSDictionary *)dict{
    if (self = [super init]) {
        
       
        _topModel = [HZGoodsDetailTopModel modelWithDictionary:[dict dictForKey:APIKEY_mall_details]];
        
        //这个是赠品
        _maxGiftNum = [[dict dictForKey:APIKEY_mall_gift] stringForKey:APIKEY_mall_maxgift nilValue:@"0"];
        _giftList = [[dict dictForKey:APIKEY_mall_gift] arrayForKey:APIKEY_mall_list];
        
        //这个是活动
        _promotion = [dict arrayForKey:APIKEY_mall_promotion];
        
        _promotionModel = [[HZGoodsDetailPromotionModel alloc] initWithGiftList:_giftList promotion:_promotion gold:_topModel.gold gold2RMB:self.topModel.gold2RMB];
    
        
        _patterns = [dict arrayForKey:APIKEY_mall_patterns];
        
        NSInteger defaultIndex = 0;
        for (NSDictionary *cell in _patterns) {
            if ([self.topModel.productID isEqualToString:[cell stringForKey:APIKEY_mall_product_id nilValue:@""]]) {
                _defaultPattern = defaultIndex;
                break;
            }
            defaultIndex ++;
        }
        
        
        _buyNotes = [dict arrayForKey:APIKEY_mall_buyNotes];

        
        
        
        _proIntro = [dict arrayForKey:APIKEY_mall_proIntro];
        
        if (_proIntro.count) {
            //专家观点
            _viewPointModel = [HZGoodsDetailViewPointModel modelWithDictionary:[_proIntro firstObject]];
        }
        
        
        [self resizeViewHeight];
        
    }
    return self;
}

- (void)resizeViewHeight{
    _headHeight = _topModel.viewHeight;
    
    _promotionViewHeight = 0;
    _specificationsViewHeight = 0;
    
    //正品 满减 优惠 包邮
    if (_promotionModel){
        _promotionViewHeight = _promotionModel.topHeight+_promotionModel.middleHeight+_promotionModel.bottomHeight;
        if (_promotionViewHeight > 0) {
            _headHeight += _promotionViewHeight + [UBO_SCREEN uh:kSizeMallViewMargin];
        }
        
    }else{
        _promotionViewHeight = 0;
    }
    //规格
    if (_patterns.count) {
        NSInteger row = _patterns.count/2.0>_patterns.count/2 ? _patterns.count/2+1 : _patterns.count/2;
        _specificationsViewHeight = [UBO_SCREEN uh:2*kSizeMallViewMargin+(kGoodsDetailTypeButtonHeight)*row oh:kSizeMallViewMargin/2*(row-1)];
        _headHeight += _specificationsViewHeight + [UBO_SCREEN uh:kSizeMallViewMargin];
    }else{
        _specificationsViewHeight = 0;
    }
    
    //月销量，送积分
    
    _headHeight += [UBO_SCREEN uh:kSizeMallViewMargin oh:kGoodsDetailExtrosViewHeight];
    
    //专家点评
    if (_viewPointModel) {
        _headHeight += [UBO_SCREEN uh:kSizeMallViewMargin oh:self.viewPointModel.veiwHeight];
    }
    
    if (_buyNotes.count) {
        //购买须知
        _buyNoticesViewHeight = [UBO_SCREEN oh:kGoodsDetailNoticeCellHeight+kSizeMallViewMargin];
        for (NSDictionary *dict in _buyNotes) {
            NSString *value = [dict stringForKey:APIKEY_mall_value nilValue:@""];
            CGFloat valueHeight = [UILabel heightForString:value font:kHZFont_Content_S width:[UBO_SCREEN.ubo fw:UB_WIDTH uw:-kSizeMallViewMargin*2 ow:-kSizeMallViewMargin/2-60-30] maxHeight:9999.0f]+1.0f;
            _buyNoticesViewHeight += valueHeight;
        }
        
        _headHeight += [UBO_SCREEN uh:kSizeMallViewMargin]+_buyNoticesViewHeight;
    }
    
    /** 隐藏评论按钮 -wirte by khzliu */
//    if([_pinglunNum integerValue]>0){
//        //评论
//        _headHeight += [UBO_SCREEN uh:kSizeMallViewMargin+kGoodsDetailTotalCmtViewHeight];
//    }
    
    _headHeight += [UBO_SCREEN uh:kSizeMallViewMargin];
}

- (void)switchPromotionView:(HZGoodsDetailPromotionModel *)model{
    
    _promotionModel = model;
    [self resizeViewHeight];
}

- (void)switchSpecificationsWithDictionary:(NSDictionary *)dict{

    
    _giftList = [[dict dictForKey:APIKEY_mall_gift] arrayForKey:APIKEY_mall_list];
    
    _promotionModel = [[HZGoodsDetailPromotionModel alloc] initWithGiftList:_giftList promotion:_promotion gold:_topModel.gold gold2RMB:self.topModel.gold2RMB];
    
    
    NSInteger defaultIndex = 0;
    for (NSDictionary *cell in _patterns) {
        if ([self.topModel.productID isEqualToString:[cell stringForKey:APIKEY_mall_product_id nilValue:@""]]) {
            _defaultPattern = defaultIndex;
            break;
        }
        defaultIndex ++;
    }
    
    [self resizeViewHeight];

}

- (void)switchViewPointView:(HZGoodsDetailViewPointModel *)model{
    self.viewPointModel = model;
    [self resizeViewHeight];
}

@end
